package com.sebscape.sebcms.tools;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletRequest;
import java.util.*;

/**
 * User: Steve
 * Date: Nov 3, 2004
 * Time: 3:59:27 PM
 */

/**
 * Utils
 */
public class Utils {

     private static Log log = LogFactory.getLog( Utils.class );

    ////////////////////////////////////////////////////////
    // Constructors
    ////////////////////////////////////////////////////////

    /**
     * Default Constructor
     */
    public Utils() {

    }

    public static boolean isEmpty( Object object ){
        if( object instanceof String ){
            String testString = (String)object;
            return ( testString == null || testString.trim().equals(""));
        } else {
            return object == null;
        }
    }

    public static String getRequestValue( ServletRequest request, String varName, String defaultValue ){
        String returnValue = request.getParameter( varName );
        if ( isEmpty(returnValue) )
            returnValue = (String)request.getAttribute( varName );
        if ( isEmpty(returnValue) )
            returnValue = defaultValue;
        return returnValue;
    }

    public static Map<String, Object> listToMap( List<String> list ){
        if( list == null )
            return null;
        Map<String, Object> returnMap = new LinkedHashMap<String, Object>();
        for ( Iterator<String> iter = list.iterator(); iter.hasNext(); returnMap.put( iter.next(), "" ) );
        return returnMap;
    }

    public static Map<String, Object> populateMapFromParametersWithPrefix( String parameterPrefix, Map<String, Object> map, Map<String, Object> parameterMap ){
        log.debug("begin...");
        // deal with the nulls.
        if ( map == null )
            return null;
        if ( parameterMap == null ||  parameterPrefix == null || parameterPrefix.equals("") )
            return map;
        // Copy the keys into a list.
        List<String> keys = new ArrayList<String>();
        for ( Iterator<String> iter = map.keySet().iterator(); iter.hasNext(); keys.add( iter.next() ) );
        log.debug("keys.size() = " + keys.size() );
        // Loop thru the map keys and check for replacement-values in the parameterMap.
        for ( int i = 0; i < keys.size(); i++ ){
            String key = (String)keys.get( i );
            log.debug("key = " + key);
            String parameterKey = parameterPrefix + key;
            log.debug("parameterKey = " + parameterKey);
            String value = ((String[])parameterMap.get( parameterKey ))[0];
            log.debug("value = " + value);
            // if the value is not null, remove the entry from the map and replace it with the new key/value.
            if ( value != null ){
                map.remove( key );
                map.put( key, value );
            }
        }
        return map;
    }

    public static Map<String, Object> populateMapFromParametersWithPrefix( String parameterPrefix, List<String> keys, Map<String, Object> parameterMap ){
        Map<String, Object> keyMap = new LinkedHashMap<String, Object>();
        // Create a Map with keys = the List entries, set their values to an empty String.
        for ( int i = 0; i < keys.size(); keyMap.put( (String)keys.get( i++ ), "" ) );
        // Now forward to the same method accepting the Map-2nd-parameter.
        return populateMapFromParametersWithPrefix( parameterPrefix, keyMap, parameterMap );
    }

    public static void main(String[] args) {
        Map<String, Object> testMap = new HashMap<String, Object>();
        testMap.put("attrib1", "value1" );
        testMap.put("attrib2", "value2" );
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("pre_attrib1", "new-value1" );
        paramMap.put("attribignore", "new-value2" );
        Map<String, Object> returnMap = populateMapFromParametersWithPrefix( "pre_", testMap, paramMap );
        for ( Iterator<String> iter = returnMap.keySet().iterator(); iter.hasNext(); ){
            String key = (String)iter.next();
            log.debug( key +" / " + returnMap.get( key ) );
        }
    }

}
